import {
  api,
  util
} from '../../../../js/util';
import {
  router
} from '../../../../js/router';
import {
  comments,
} from '../../../../js/modules/mall';
import BasePage from '../../../../js/common/base-page'
const app = getApp()
BasePage({
  pageKey: "productCommentList",
  validateLogin: false,
  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    tablist: [],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 10,
    slidewidth: 0,
    lists: [{
        listType: "All", //全部
        list: [],
        hasMoreData: true,
        page: {
          PageIndex: 1,
          PageSize: 20,
          keywords: "",
          sortField: "AddTime",
          sortDirect: "desc"
        }
      },
      {
        listType: "Praise", //好评
        list: [],
        hasMoreData: true,
        page: {
          PageIndex: 1,
          PageSize: 20,
          keywords: "",
          sortField: "AddTime",
          sortDirect: "desc"
        }
      }, {
        listType: "Neutral", //中评
        list: [],
        hasMoreData: true,
        page: {
          PageIndex: 1,
          PageSize: 20,
          keywords: "",
          sortField: "AddTime",
          sortDirect: "desc"
        }
      }, {
        listType: "Poor", //差评
        list: [],
        hasMoreData: true,
        page: {
          PageIndex: 1,
          PageSize: 20,
          keywords: "",
          sortField: "AddTime",
          sortDirect: "desc"
        }
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    this.data.pid = options.pid || 0;
    this.data.fkId = app.dns.proprietorId;
    this.data.fkFlag = app.dns.proprietor;
    this.loadCommentInfo();
    this.loadList();

    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          slidewidth: (res.windowWidth / that.data.tablist.length) - 20,
        });
      }
    });
  },
  /**加载评论 */
  loadCommentInfo(){
    var that = this;
    comments.getCommentsInfo({
      gid: 0,
      pid: this.data.pid,
      fkId: this.data.fkId,
      fkFlag: this.data.fkFlag
    }, function (content) {
      var content = content.Content;
      that.setData({
        tablist: ['全部评论' + content.Count, '好评' + content.Praise, '中评' + content.Neutral, '差评'+content.Poor]
      });
    });
  },
  /**加载商品列表 */
  loadList() {
    var that = this;
    var current = this.data.lists[that.data.activeIndex];
    var param = current.page;
    comments.getProductCommentList({
      listType: current.listType,
      pid: this.data.pid,
      fkId: this.data.fkId,
      fkFlag: this.data.fkFlag
    }, param, function(content) {
      var hasMoreData = content.count > (current.page.PageIndex) * current.page.PageSize;
      var list_key = "lists[" + that.data.activeIndex + "].list";
      //是否有更多数据
      var list_has = "lists[" + that.data.activeIndex + "].hasMoreData";
      //无数据时显示的信息
      var isLoadComplete = "lists[" + that.data.activeIndex + "].isLoadComplete";
      var index = that.data.lists[that.data.activeIndex].list.length;
      var data = content.data;
      //当分页大于1时，累加
      if (current.page.PageIndex > 1) {
        data = that.data.lists[that.data.activeIndex].list.concat(data);
      }
      //如果key为变量作为key值的时候要用[]包裹起来
      that.setData({
        [list_key]: data,
        [list_has]: hasMoreData,
        [isLoadComplete]: true
      });


    });

  },
  //图片点击事件
  imgYu: function (event) {
    var src = event.currentTarget.dataset.src;//获取data-src
    var imgList = event.currentTarget.dataset.list;//获取data-list
    var imgSrl=[];
    for (var i = 0; i < imgList.length;i++){
      imgSrl[i] = this.data.imgDomain+imgList[i].VirtualPath;
    }
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgSrl // 需要预览的图片http链接列表
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
    this.data.lists[this.data.activeIndex].page.PageIndex = 1;
    this.loadList();
    // console.log("onPullDownRefresh");
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function(e) {
    var current = this.data.lists[this.data.activeIndex];
    if (current.hasMoreData) {
      var index = this.data.lists[this.data.activeIndex].page.PageIndex;
      this.data.lists[this.data.activeIndex].page.PageIndex = index + 1;
      this.loadList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 切换当前选择项
   */
  onSwitchItem: function(res) {
    if (this.data.activeIndex == res.detail.currentNum.id) return;
    var activeIndex = res.detail.currentNum.id;
    this.setData({
      sliderOffset: res.detail.currentNum.offsetLeft,
      activeIndex: activeIndex
    });
    var current = this.data.lists[activeIndex];
    // if (current.list == null || current.list.length == 0) {
    this.loadList();
    // }
  },
})